package com.example.demo.server.common;

import com.example.demo.client.common.CommonRequest;
import com.example.demo.client.common.ViewKey;
import com.example.demo.ds.MyList;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;


public class APPUtils {

    public static boolean hasLength(String str) {
        return str != null && !str.isEmpty();
    }

    public static boolean isAllHasLength(String... strings) {
        for(String x : strings) {
            if(!hasLength(x)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 拆分命令信息
     * @param command
     * @return
     */
    public static MyList<String> handleCommand(String command) {
        String[] strings = command.split(" ");
        MyList<String> myList = new MyList<>();
        for(String x : strings) {
            if(!x.equals("")) {
                myList.add(x);
            }
        }
        //将第一个命令元转化为全小写
        if(myList.size() >= 1) {
            myList.set(0, myList.get(0).toLowerCase());
        }
        return myList;
    }


    public static void printPopUp(String msg) {
        int length = msg.length();
        System.out.print("+");
        for (int i = 0; i < length; i++) {
            System.out.print("-");
        }
        System.out.println("+");
        System.out.println("|" + msg + "|");
        System.out.print("+");
        for (int i = 0; i < length; i++) {
            System.out.print("-");
        }
        System.out.println("+");
    }

    public static String input() {
        String command = null;
        do {
            System.out.print("[COMMAND]:> ");
            command = APPVariable.SCANNER.nextLine();
        }while(command.trim().equals(""));

        return command;
    }

    public static String titleInput() {
        String command = null;
        System.out.print("[TITLE]:> ");
        command = APPVariable.SCANNER.nextLine();
        return command;
    }
    public static String summaryInput() {
        String command = null;
        System.out.print("[SUMMARY]:> ");
        command = APPVariable.SCANNER.nextLine();
        return command;
    }
    public static String contentInput() {
        String command = "";
        System.out.println("[Content]:> ");
        while (true) {
            String input = APPVariable.SCANNER.nextLine();
            if(input.equals("")) {
                break;
            }
            command += input + "\n";
        }
        return command;
    }


    public static CommonRequest checkJump(String view, String... views) {
        for(String x : views) {
            if(x.equals(view)) {
                if(x.equals(ViewKey.LOGOUT)) {
                    SessionUtils.removeSession();
                    SessionUtils.removeCookie();
                    return CommonRequest.createRequest(ViewKey.LOGIN);
                }
                return CommonRequest.createRequest(view);
            }
        }
        return null;
    }

    public static String now() {
        return new SimpleDateFormat(APPVariable.DATA_FORMAT)
                .format(new Timestamp(System.currentTimeMillis()));
    }

}
